package com.alcatel_lucent.osp.action;

import com.alcatel_lucent.osp.model.Category;
import com.alcatel_lucent.osp.model.RequestStatus;
import com.alcatel_lucent.osp.model.Type;
import com.alcatel_lucent.osp.service.DaoService;
import com.alcatel_lucent.osp.service.RequestServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: guoshunw
 * Date: 13-8-12
 * Time: 下午1:19
 * To change this template use File | Settings | File Templates.
 */
@SuppressWarnings("serial")
public class IndexAction extends BaseAction implements SessionAware {

    private Map<String, Object> session;
    private Boolean isLogin;

    private Integer category;
    private Long requestId;
    private Integer viewMode;

    private DaoService daoService;

    private HttpServletRequest request = ServletActionContext.getRequest();

    public DaoService getDaoService() {
        return daoService;
    }

    public Boolean getLogin() {
        return isLogin;
    }

    @Value("${httpsPort}")
    private String httpsPort;

    public String getHttpsPort() {
        return httpsPort;
    }

    public Collection<Category> getCategories() {
        return daoService.retrieve("from Category");
    }

    public Collection<Type> getTypes() {
        if (category != null) {
            return daoService.retrieve("select c.types from Category c where c.id=" + category);
        } else {
            return new ArrayList<Type>();
        }
    }

    @Override
    public String execute() {

        isLogin = null != session.get(UserContext.SESSION_USER_CONTEXT);
        // to login page

        ActionContext.getContext().put("now", new Date());
        log.info("is login: ={}, httpsPort={}" , isLogin, getHttpsPort());

        // remove temp upload attachments
        session.remove(UploadAttachmentAction.UPLOAD_TOKEN_KEY);
        session.remove(UploadAttachmentAction.TRANSIENT_ATTACHMENT_ID_KEY);

        return SUCCESS;
    }

    public Map<String, String> getRequestStatus() {
        Map<String, String> requestStatus = new HashMap<String, String>();
        for (RequestStatus status : RequestStatus.values()) {
            requestStatus.put(status.name(), status.name());
        }
        return requestStatus;
    }

    public String getGlobalObjects() {
        Map<String, Object> globalMap = new HashMap<String, Object>();
        globalMap.put("PROJECT_NAME_EXT", RequestServiceImpl.rd360ProgramTypeMapping);

        Map<String, Integer> viewModelMap = new HashMap<String, Integer>();
        viewModelMap.put("REQUEST", ViewRequestAction.VIEW_MODE_REQUEST);
        viewModelMap.put("FOLLOWUP", ViewRequestAction.VIEW_MODE_FOLLOWUP);
        viewModelMap.put("EMAIL", ViewRequestAction.VIEW_MODE_EMAIL);
        globalMap.put("VIEW_MODE", viewModelMap);

        String jsonString = JSONObject.fromObject(globalMap).toString(4);
        log.info("globalObjects={}", jsonString);
        return jsonString;
    }


    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }


    public Integer getCategory() {
        return category;
    }


    public void setCategory(Integer category) {
        this.category = category;
    }


    public Long getRequestId() {
        return requestId;
    }


    public void setRequestId(Long requestId) {
        this.requestId = requestId;
    }


    public Integer getViewMode() {
        return viewMode;
    }


    public void setViewMode(Integer viewMode) {
        this.viewMode = viewMode;
    }

    public void setDaoService(DaoService daoService) {
        this.daoService = daoService;
    }
}
